<template>
  <view class="container">
    <!-- 左侧省份列表 -->
    <view class="scroll-container">
      <view class="list">
        <view
          v-for="(province, index) in provinces"
          :key="index"
          class="list-item"
          :class="{'selected': selectedProvinceIndex === index}"
          @click="selectProvince(index)"
        >
          {{ province.province }}
        </view>
      </view>
    </view>

    <!-- 右侧城市列表 -->
    <view class="scroll-container">
      <view class="list">
        <view v-if="selectedProvince">
          <view
            v-for="(city, index) in selectedProvince.city"
            :key="index"
            class="list-item"
            :class="{'selected': selectedCity === city}"
            @click="selectCity(city)"
          >
            {{ city }}
          </view>
        </view>
      </view>
    </view>
    <!-- 底部固定按钮 -->
    <view class="button-container">
      <button class="reset-button" @click="resetSelection">重置</button>
      <button class="confirm-button" @click="confirmSelection">确认</button>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapActions } from 'vuex';

export default {
  data() {
    return {
      selectedProvinceIndex: null,
      selectedCity: null,
      type: 'job' // 默认类型为'job'
    };
  },

  computed: {
    ...mapGetters(['provinceList']),
    selectedProvince() {
      if (this.selectedProvinceIndex !== null) {
        return this.provinceList[this.selectedProvinceIndex];
      }
      return null;
    },
    provinces() {
      return this.provinceList.map(province => ({
        province: province.province,
        city: province.city
      }));
    }
  },
  methods: {
    ...mapActions(['getProvinceList']),
    selectProvince(index) {
      this.selectedProvinceIndex = index;
      this.selectedCity = null; // 重置选中的城市
    },
    selectCity(city) {
      this.selectedCity = city;
    },
    confirmSelection() {
      if (this.selectedCity) {
        // 触发全局事件，通知父组件城市已选择
        uni.$emit('city-selected', {
          city: this.selectedCity,
          type: this.type
        });
        uni.navigateBack();
      } else {
        uni.showToast({
          title: '请选择一个城市',
          icon: 'none'
        });
      }
    },
    resetSelection() {
      this.selectedProvinceIndex = null; // 重置省份索引
      this.selectedCity = null; // 重置城市
      // 触发全局事件，通知父组件城市已重置
      uni.$emit('city-reset', {
        type: this.type
      });
      uni.navigateBack(); // 返回上一页
    }
  },
  mounted() {
    this.getProvinceList();
  },
  onLoad(options) {
    // 接收type参数，默认为'job'
    if (options.type) {
      this.type = options.type;
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  height: calc(100vh - 80px); /* 减去按钮的高度 */
}

.scroll-container {
  width: 50%;
  overflow-y: auto; /* 设置垂直滚动 */
  background-color: #ffffff;
  padding: 20px;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.list {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.list-item {
  width: 100%;
  padding: 15px 0;
  border-bottom: 1px solid #e8e8e8;
  font-size: 18px;
  color: #333;
  cursor: pointer;
  transition: color 0.3s, background-color 0.3s;
  text-align: center;
}

.list-item.selected {
  color: #409eff; /* 选中的字体颜色 */
  font-weight: bold;
}

.list-item:hover {
  color: #409eff;
}

.list-item:last-child {
  border-bottom: none;
}

/* 底部固定按钮容器 */
.button-container {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  background-color: #ffffff;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.05);
  z-index: 10;
}

/* 按钮通用样式 */
.button-container button {
  width: 48%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 12px;
  border: none;
  outline: none;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 重置按钮样式 */
.reset-button {
  background-color: #f5f7fa;
  color: #4e5969;
}

.reset-button:hover {
  background-color: #eef1f5;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.reset-button:active {
  transform: translateY(0);
  box-shadow: none;
}

/* 确认按钮样式 - 使用渐变和阴影增强质感 */
.confirm-button {
  background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.25);
}

.confirm-button:hover {
  background: linear-gradient(135deg, #3689e6 0%, #59a8f8 100%);
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);
}

.confirm-button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
}
</style>